<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button size="mini" style="float:right" type="primary" @click="getDataList()">
        查询
      </el-button>
      <el-button size="mini" style="float:right;margin-right: 15px" @click="handleResetSearch()">
        重置
      </el-button>
      <el-button size="mini" class="btn-add" type="danger" style="margin-left: 20px" @click="deleteHandle()" :disabled="dataListSelections.length <= 0" >批量删除</el-button>
      <el-button size="mini" class="btn-add" type="primary" @click="addOrUpdateHandle()">新增</el-button>
    </el-card>
    <div class="table-container">
      <el-table :data="list" border v-loading="dataListLoading" @selection-change="selectionChangeHandle" style="width: 100%;">
        <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="id" label="行政地区编号" header-align="center" align="center"></el-table-column>
          <el-table-column prop="name" label="地区名称" header-align="center" align="center"></el-table-column>
          <el-table-column label="地区级别" align="center">
            <template slot-scope="scope">{{ scope.row.level | levelFilter }}</template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" header-align="center" align="center"></el-table-column>
          <el-table-column prop="updateTime" label="更新时间" header-align="center" align="center"></el-table-column>
          <el-table-column label="设置" align="center">
            <template slot-scope="scope">
              <el-button size="mini" type="text" :disabled="scope.row.level | disableNextLevel"
                @click="handleShowNextLevel(scope.$index, scope.row)">查看下级
              </el-button>
            </template>
          </el-table-column>
        <el-table-column label="操作" fixed="right" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button size="mini" type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页控件 -->
    <div class="pagination-container">
      <el-pagination
        background
        class="table-pagination"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import { fetchRegionList, deleteRegion } from '@/api/sys/region';
import AddOrUpdate from './region-add-or-update';
const baseUrl = process.env.VUE_APP_FILE_URL === '/' ? '' : process.env.VUE_APP_FILE_URL;

const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
};

export default {
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      baseApi: baseUrl,
      parentId: 0,
      preLevel: 1,
      preId: '0',
      preName: '',
    }
  },
  components: {
    AddOrUpdate
  },
  created() {
    this.resetParentId();
    this.getDataList();
  },
  watch: {
    $route(route) {
      this.resetParentId();
      this.getDataList();
    }
  },
  methods: {
    resetParentId() {
      this.listQuery.pageNum = 1;
      if (this.$route.query.parentId != null) {
        this.parentId = this.$route.query.parentId;
      } else {
        this.parentId = 0;
      }
    },
    // 重置查询表单
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      if(this.parentId == 0) this.preLevel = 1;
      this.dataListLoading = true
      fetchRegionList(this.parentId, this.listQuery).then(response => {
        this.dataListLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.listQuery.pageNum = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    handleShowNextLevel(index, row) {
      this.preLevel = Number(row.level) + 1;
      this.preName = row.name;
      this.preId = row.id;
      this.$router.push({ path: '/sys/region', query: { parentId: row.id } });
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, this.preLevel, this.preId, this.preName)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
       this.$confirm(`确定对[id=${ids.join(',')}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRegion(ids).then(res => {
          if (res && res.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000
            })
            this.getDataList()
          } else {
             this.$message.error(res.msg) 
          }
        })
      }).catch(() => {})
    },
  },
  filters: {
    levelFilter(value) {
      if (value === '1') {
        return '省份';
      } else if (value === '2') {
        return '城市';
      } else if (value === '3') {
        return '地区';
      }
    },
    disableNextLevel(value) {
      if (value === '1') {
        return false;
      } else if(value === '2') {
        return false;
      } else {
        return true;
      }
    }
  }
}
</script>
